package com.xy.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xy.constant.Constant;
import com.xy.entity.Company;
import com.xy.entity.LoanPeriod;
import com.xy.entity.Product;
import com.xy.entity.ProductType;
import com.xy.entity.User;
import com.xy.service.CompanyService;
import com.xy.service.LoanPeriodService;
import com.xy.service.ProductService;
import com.xy.service.ProductTypeService;
import com.xy.util.PropertiesUtil;

@Controller
public class ProductController {
	private Logger log = Logger.getLogger(ProductController.class);
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductTypeService productTypeService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private LoanPeriodService loanPeriodService;

	@RequestMapping(value = "toAddProduct", method = RequestMethod.GET)
	public String toAddProduct(Model model) {
		List<ProductType> productTypeList = productTypeService.selectProductType(null);
		List<Company> companyList = companyService.selectCompanyList(null);
		List<LoanPeriod> loanPeriodList = loanPeriodService.selectLoanPeriod(null);
		model.addAttribute("productTypeList", productTypeList);
		model.addAttribute("companyList", companyList);
		model.addAttribute("loanPeriodList", loanPeriodList);
		return "pages/backend/productAdd.jsp";
	}

	/**
	 * 添加产品信息
	 * 
	 * @param product
	 * @param model
	 * @param session
	 * @param result
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "addProduct", method = { RequestMethod.POST, RequestMethod.GET })
	public String addProduct(Product product, HttpSession session, BindingResult result, ModelMap modelMap) {
		// 赋值
		User user = (User) session.getAttribute("myUser");
		product.setCreaterID(user.getID());
		product.setCreaterTime(new Date());
		// 获取错误信息
		Map<String, String> errorMap = new HashMap<String, String>();
		if (result.hasErrors()) {
			List<FieldError> fieldErrorList = result.getFieldErrors();
			for (FieldError fieldError : fieldErrorList) {
				String fieldName = fieldError.getField();
				String errorMessage = fieldError.getDefaultMessage();
				log.info("fieldName:" + fieldName + ", errorMessage:" + errorMessage);
				errorMap.put(fieldName, errorMessage);
			}
		}
		if (productService.addProduct(product)) {
			log.info("产品信息添加成功:" + product);
		}

		modelMap.addAttribute("errorMap", errorMap);
		return "selectProduct.action";
	}

	/**
	 * 删除产品信息
	 * 
	 * @param product
	 * @return
	 */
	@RequestMapping(value = "deleProduct", method = RequestMethod.GET)
	public @ResponseBody String deleProduct(Product product) {
		if (productService.deleProduct(product)) {
			log.info("产品删除成功");
			return "success";
		}
		return "error";
	}

	/**
	 * 更新产品信息
	 * 
	 * @param product
	 * @param session
	 * @param result
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "udateProduct", method = RequestMethod.POST)
	public String updateProduct(Product product, HttpSession session, BindingResult result, ModelMap modelMap) {
		// 赋值
		User user = (User) session.getAttribute("myUser");
		product.setCreaterID(user.getID());
		product.setCreaterTime(new Date());
		// 获取错误信息
		Map<String, String> errorMap = new HashMap<String, String>();
		if (result.hasErrors()) {
			List<FieldError> fieldErrorList = result.getFieldErrors();
			for (FieldError fieldError : fieldErrorList) {
				String fieldName = fieldError.getField();
				String errorMessage = fieldError.getDefaultMessage();
				log.info("fieldName:" + fieldName + ", errorMessage:" + errorMessage);
				errorMap.put(fieldName, errorMessage);
			}
		}
		if (productService.updateProduct(product)) {
			log.info("产品信息更新成功:" + product);
		}
		modelMap.addAttribute("errorMap", errorMap);
		return "selectProduct.action";
	}

	@RequestMapping(value = "selectProductDetail", method = RequestMethod.GET)
	public String selectProductDetail(Product product, Model model, int code) {
		List<Product> productList = productService.selectProduct(product);
		product = productList.get(0);
		model.addAttribute("product", product);
		// code=0,到详情界面;1到修改界面
		if (code == 0) {
			return "pages/backend/productDetail.jsp";
		} else {
			List<ProductType> productTypeList = productTypeService.selectProductType(null);
			List<Company> companyList = companyService.selectCompanyList(null);
			List<LoanPeriod> loanPeriodList = loanPeriodService.selectLoanPeriod(null);
			model.addAttribute("productTypeList", productTypeList);
			model.addAttribute("companyList", companyList);
			model.addAttribute("loanPeriodList", loanPeriodList);
			return "pages/backend/productModify.jsp";
		}

	}

	/**
	 * 查询所有产品信息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "selectProduct", method = { RequestMethod.GET, RequestMethod.POST })
	public String selectProduct(Model model) {
		List<Product> productList = productService.selectProduct(null);
		model.addAttribute("productList", productList);
		return "pages/backend/productManage.jsp";
	}

	/**
	 * 融资产品主页
	 * 
	 * @param model
	 * @param session
	 * @return
	 */
	// index.jsp
	@RequestMapping(value = "selectFinancingProduct", method = RequestMethod.GET)
	public String selectFinancingProduct(Model model, HttpSession session) {
		session.setAttribute("resourse", PropertiesUtil.getValue(Constant.TOMCAT_PATH));
		// 产品类型
		List<ProductType> productTypeList = productTypeService.selectProductType(null);
		// 产品
		List<Product> productList = productService.selectProduct(null);
		// 银行
		List<Company> companyList = companyService.selectCompanyList(null);
		// 贷款周期
		List<LoanPeriod> loanPeriodList = loanPeriodService.selectLoanPeriod(null);
		model.addAttribute("productTypeList", productTypeList);
		model.addAttribute("productList", productList);
		model.addAttribute("companyList", companyList);
		model.addAttribute("loanPeriodList", loanPeriodList);
		return "pages/index.jsp";
	}

	/**
	 * 筛选条件JSON
	 * 
	 * @param product
	 * @return
	 */
	@RequestMapping(value = "selectAssignProduct", method = RequestMethod.GET)
	public @ResponseBody List<Product> selectAssignProduct(Product product) {
		List<Product> productList = productService.selectProduct(product);
		return productList;
	}

	/**
	 * 查看产品详情,用于在线申请产品
	 * 
	 * @param product
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "selectProductDetailOnline", method = RequestMethod.GET)
	public String selectProductDetailOnline(Product product, Model model) {
		List<Product> productList = productService.selectProduct(product);
		product = productList.get(0);
		model.addAttribute("product", product);
		return "pages/product.jsp";
	}

}
